package org.dolphin.dao.impl;

import java.util.List;
import org.dolphin.base.bean.DueDatePeriodVO;
import org.dolphin.dao.DueDatePeriodDao;
import org.dolphin.dao.util.SessionHelper;
import org.dolphin.entity.misc.DueDatePeriod;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "dueDatePeriodDao")
public class DueDatePeriodDaoImpl implements DueDatePeriodDao {
  @Autowired(required = true)
  private SessionFactory sessionFactory;

  protected DetachedCriteria buildDueDatePeriodCriteria() {
    DetachedCriteria dc = DetachedCriteria.forClass(DueDatePeriod.class);
    return dc;
  }

  @Override
  public void deleteDueDatePeriod(DueDatePeriod dueDatePeriod) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    session.delete(dueDatePeriod);
  }

  @Override
  public DueDatePeriod getDueDatePeriod(String code) {
    return getDueDatePeriod(code, null);
  }

  @SuppressWarnings("unchecked")
  @Override
  public DueDatePeriod getDueDatePeriod(String code, Long excludedId) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    DetachedCriteria dc = buildDueDatePeriodCriteria();
    dc.add(Restrictions.eq("code", code));
    if (excludedId != null) {
      dc.add(Restrictions.not(Restrictions.idEq(excludedId)));
    }
    Criteria criteria = dc.getExecutableCriteria(session);
    List<DueDatePeriod> list = criteria.list();
    if (list != null && !list.isEmpty()) {
      return list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<DueDatePeriodVO> getDueDatePeriods(int firstResult, int maxResults, List<Order> orders) {
    DetachedCriteria dc = buildDueDatePeriodCriteria();
    if (orders != null && !orders.isEmpty()) {
      for (Order order : orders) {
        dc.addOrder(order);
      }
    }
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.id(), "id");
    projectionList.add(Projections.property("code"), "code");
    projectionList.add(Projections.property("year"), "year");
    projectionList.add(Projections.property("month"), "month");
    projectionList.add(Projections.property("week"), "week");
    projectionList.add(Projections.property("day"), "day");
    projectionList.add(Projections.property("version"), "version");
    dc.setProjection(projectionList);
    dc.setResultTransformer(new AliasToBeanResultTransformer(DueDatePeriodVO.class));
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = dc.getExecutableCriteria(session);
    if (firstResult > 0) {
      criteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      criteria.setMaxResults(maxResults);
    }
    List<DueDatePeriodVO> list = criteria.list();
    return list;
  }

  @Override
  public Long getDueDatePeriodsCount() {
    DetachedCriteria dc = buildDueDatePeriodCriteria();
    dc.setProjection(Projections.rowCount());
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = dc.getExecutableCriteria(session);
    Number totalRecords = (Number) criteria.uniqueResult();
    return totalRecords.longValue();
  }

  @Override
  public void saveDueDatePeriod(DueDatePeriod dueDatePeriod) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    session.save(dueDatePeriod);
  }
}
